/**
 * common/router
 */

import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

// import XSB from './xsb';
import routes from '../routes';

const router = new VueRouter({
    mode: 'history',
    routes
});

router.afterEach(to => {
    const paths = to.path.split('/').slice(1, 3).map(name => {
        if(!name) {
            return 'index';
        }

        if(+name > 0) {
            return 'item';
        }

        return name;
    });
    const className = `page-${paths[0]} page-${paths[0]}-${paths[1] || 'index'}`;
    document.documentElement.className = className;

    // title
    document.title = to.meta.title || '小柿饼';

    // debug
    if(process.env.NODE_ENV === 'development') {
        Vue.nextTick(() => {
            const main = document.querySelector('.main');

            window.vm = main && main.__vue__ || null;

            window.jQuery = window.$ = require('jquery');
            window.Vue = Vue;
        });
    }
});

export default router;
